<template>
  <div>
    <el-form inline :model="queryBard" ref="bardQueryFormRef">
      <el-form-item>
        <el-input placeholder="关键子" v-model="queryBard.keywords"></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="queryBard.status" placeholder="状态" style="width: 240px">
          <el-option label="全部" :value="-1"/>
          <el-option label="正常" :value="0"/>
          <el-option label="禁用" :value="1"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button :icon="Search" @click="getSocBardList">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button :icon="Refresh" @click="bardQueryFormRef?.resetFields()"
        >重置
        </el-button
        >
      </el-form-item>
    </el-form>
    <el-row>
      <el-col :span="2">
        <el-button :icon="Plus" @click="dialogVisible = true" size="small" type="primary">新增</el-button>
      </el-col>
    </el-row>
    <el-table :data="socBardList" v-loading="loading">
      <el-table-column label="显示" width="90px">
        <template #default="scope">
          <el-icon size="50">
            <el-image :src="scope.row.cover"></el-image>
          </el-icon>
        </template>
      </el-table-column>
      <el-table-column prop="bardId" label="编号" width="70px"/>
      <el-table-column prop="bardName" label="名称"/>
      <el-table-column prop="schoolId" label="学校编号"/>
      <el-table-column prop="description" label="简介"/>
      <el-table-column prop="createTime" label="创建时间"/>
      <el-table-column prop="status" label="状态" width="70px"/>
      <el-table-column label="操作" width="250px">
        <template #default="scope">
          <el-button :icon="EditPen" @click="openModify(scope.row.bardId)" size="small" type="warning">修改</el-button>
          <el-divider direction="vertical"/>
          <el-button :icon="Delete" type="danger" size="small" @click="openDel(scope.row.bardId)">删除</el-button>
          <el-divider direction="vertical"/>
          <el-dropdown>
            <el-button class="el-dropdown-link" type="primary" size="small">
              更多
              <el-icon class="el-icon--right">
                <arrow-down/>
              </el-icon>
            </el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="publishPost(scope.row.bardId)"
                >发帖
                </el-dropdown-item
                >
                <el-dropdown-item @click="openView(scope.row.bardId)">预览</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        v-model:current-page="queryBard.pageNum"
        :page-sizes="[8, 16, 32, 64]"
        v-model:page-size="queryBard.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
    />
    <el-dialog
        :title="isAdd ? '新增' : '修改'"
        v-model="dialogVisible"
        width="500"
        @close="socBardFormRef?.resetFields"
    >
      <el-form :model="socBardForm" ref="socBardFormRef" :rules="rules" label-width="80">
        <el-form-item label="吧名" prop="bardName">
          <el-input v-model="socBardForm.bardName"></el-input>
        </el-form-item>
        <el-form-item label="校园编号" prop="schoolId">
          <el-input v-model="socBardForm.schoolId"></el-input>
        </el-form-item>
        <el-form-item label="封面" prop="cover">
          <upload-button url="/bard" @filePath="uploadSuccess"></upload-button>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input type="textarea" v-model="socBardForm.description"></el-input>
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker
              v-model="socBardForm.createTime"
              type="datetime"
              placeholder="选择日期时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="socBardForm.status">
            <el-radio :value="0">正常</el-radio>
            <el-radio :value="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitSocBard(socBardFormRef)">保存</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog title="帖子预览" v-model="viewDialogVisible" width="500">
      <div></div>
      <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="viewDialogVisible = false">确 定</el-button>
      </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import {ref, reactive, onMounted, watch} from "vue";
import {useRouter} from "vue-router";
import {Search, Refresh, Delete, EditPen, Plus, View, ArrowDown} from "@element-plus/icons-vue";
import type {FormInstance, FormRules} from "element-plus";
import {ElMessage, ElMessageBox} from "element-plus";
import type {SocBard} from "@/api/social/bard/type";
import {
  reqCreateBard,
  reqUpdateBard,
  reqGetBardList,
  reqGetBardById,
  reqDeleteBard,
} from "@/api/social/bard";
import UploadButton from "@/components/common/UploadButton.vue";

let queryBard = reactive({
  pageNum: 1,
  pageSize: 8,
  keywords: "",
  status: -1,
});
let loading = ref<boolean>(true);
let socBardForm = reactive<SocBard>({
  bardId: null,
  bardName: "",
  schoolId: "",
  userId: "",
  cover: "",
  description: "",
  createTime: null,
  status: 0,
});
let total = ref<number>(0);
let rules = reactive<FormRules<SocBard>>({
  bardName: [
    {
      required: true,
      message: "请输入吧名",
      trigger: "blur",
    },
  ],
  schoolId: [
    {
      required: true,
      message: "请输入校园编号",
      trigger: "blur",
    },
  ],
});
const router = useRouter();
let dialogVisible = ref(false);
let isAdd = ref(true);
let viewDialogVisible = ref();
let socBardFormRef = ref<FormInstance>();
let bardQueryFormRef = ref<FormInstance>();
let socBardList = ref<SocBard[]>([]);

let getSocBardList = () => {
  reqGetBardList(queryBard).then((res) => {
    socBardList.value = res.data.list;
    total.value = res.data.total;
    loading.value = false;
  });
};
let uploadSuccess = (filepath: string) => {
  socBardForm.cover = filepath;
};
let openModify = (bardId: string) => {
  reqGetBardById(bardId).then((res) => {
    Object.assign(socBardForm, res.data);
    isAdd.value = false;
    dialogVisible.value = true;
  });
};
let openView = (bardId: string) => {
};
let submitSocBard = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      if (isAdd.value) {
        await reqCreateBard(socBardForm);
      } else {
        await reqUpdateBard(socBardForm);
        isAdd.value = true;
      }
      await getSocBardList();
      dialogVisible.value = false;
    } else console.log("error submit!", fields);
  });
};
let openDel = (bardId: string) => {
  ElMessageBox.confirm("删除之后不能恢复，确认删除吗？", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
      .then(() => {
        reqDeleteBard(bardId).then(() => {
          getSocBardList();
          ElMessage({
            message: "删除成功！",
            type: "success",
          });
        });
      })
      .catch(() => {
        ElMessage({
          type: "info",
          message: "取消删除！",
        });
      });
};
let publishPost = (bardId: string) => {
  router.push({
    path: "/layout/social/soc-post",
    query: {
      bardId,
    },
  });
};
onMounted(() => {
  getSocBardList();
});

watch([() => queryBard.pageNum, () => queryBard.pageSize], () => {
  getSocBardList();
});
</script>
